package cloudInformation.interceptor;

import cloudInformation.annotate.AuthCheck;
import cloudInformation.exception.PermissionException;
import huang.cloudUserApi.AuthFeignClient;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @program: Smart School
 * @ClassName AuthCheckInterceptor
 * @description: AuthCheck注解 鉴权切面类
 * @author: 陈智博
 * @create: 2025-04-15 15:39
 * @Version 1.0
 **/
@Aspect
@Component
public class AuthCheckInterceptor {

    @Autowired
    private AuthFeignClient authFeignClient;

    @Pointcut("@annotation(cloudInformation.annotate.AuthCheck)")
    public void Check(){}


    /**
     * 前置通知 开始
     * @param joinPoint 切入 点
     * @param checkInfo 获取切面的值
     */
    @Around("Check() && @annotation(checkInfo)")
    public Object doInterceptor(ProceedingJoinPoint joinPoint, AuthCheck checkInfo) throws Throwable {
        List<String> userPermission = authFeignClient.getUserPermission();
        /**
         * 如果权限集合为空
         */
        if(userPermission.isEmpty())
        {
            throw new PermissionException();
        }
        /**
         * 这里 鉴权 判断 是否 拥有 权限
         */
        for(String arr : checkInfo.values())
        {
            if(userPermission.contains(arr))
            {
                return joinPoint.proceed();
            }
        }

        throw new PermissionException();
    }


}
